<template>
  <div class="ele-body">
    <el-card shadow="never">
 
      <div slot="header" class="clearfix">
        <span>市场管理</span>
      </div>

      <!-- 搜索表单 -->
      <el-form
        :model="where"
        class="ele-form-search"
        label-width="75px"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <div class="app-container">
          <div class="filter-container">
            <el-input
              v-model="where.name"
              class="filter-item"
              clearable
              placeholder="市场名称" style="width: 200px;"
              @clear="reload"/>

            <el-select
              v-model="where.platform"
              class="filter-item"
              clearable
              placeholder="请选择推广平台"
              @change="reload"
              @clear="reload">
              <el-option
                v-for="item in platformList"
                :key="item.value"
                :label="item.label"
                :value="item.value"/>
            </el-select>

            <el-select
              v-model="where.model"
              class="filter-item"
              clearable
              placeholder="请选择展示模式"
              @change="reload"
              @clear="reload">
              <el-option
                v-for="item in modelList"
                :key="item.value"
                :label="item.label"
                :value="item.value"/>
            </el-select>

            <el-button

              class="filter-item"
              icon="el-icon-search"
              type="primary"
              @click="reload">搜索
            </el-button>

            <el-button class="filter-item" icon="el-icon-refresh" style="margin-left: 2px;" @click="reset">重置
            </el-button>

          </div>
        </div>
      </el-form>

      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="url"
        :max-height="defaultHeight"
        :selection.sync="selection"
        :where="where">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            v-if="$hasPermission('bazaar:save')"

            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="openEdit({})">添加
          </el-button>
          <el-button
            v-if="$hasPermission('bazaar:delete')"

            icon="el-icon-delete"
            size="small"
            type="danger"
            @click="removeBatch">删除
          </el-button>
        </template>
        <!-- 推广平台列 -->
        <template slot="platform" slot-scope="{row}">
          <template v-for="item in platformList">
            <el-tag
              v-if="item.value === row.platform"
              :key="item.value"
              :disable-transitions="true"
              size="mini"
              type="primary">
              {{ item.label }}
            </el-tag>
          </template>
        </template>
        <!-- 展示模式列 -->
        <template slot="model" slot-scope="{row}">
          <template v-for="item in modelList">
            <el-tag
              v-if="item.value === row.model"
              :key="item.value"
              :disable-transitions="true"
              :type="['','success','info'][row.model]"
              size="mini">
              {{ item.label }}
            </el-tag>
          </template>
        </template>
        <!-- 状态列 -->
        <template slot="status" slot-scope="{row}">
          <el-switch
            v-model="row.status"
            :active-value="1"
            :inactive-value="0"
            @change="editState(row)"/>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            v-if="$hasPermission('bazaar:update')"
            :underline="false"
            icon="el-icon-edit-outline"
            type="primary"
            @click="openEdit(row)">修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此用户吗？"
            @confirm="remove(row)">
            <el-link
              v-if="$hasPermission('bazaar:delete')"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              type="danger">删除
            </el-link>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <vue-edit
      :data="current"
      :visible.sync="showEdit"
      @done="reload"/>
  </div>
</template>

<script>
import VueEdit from './vue-edit';

export default {
  name: 'siteBazaar',
  components: {VueEdit},
  data() {
    return {
      // 表格高度
      defaultHeight: 0,
      // 展示平台
      platformList: [{value: 1, label: '百度SEM竞价'}
        , {value: 2, label: '百度信息流'}
        , {value: 3, label: '巨量引擎'}
        , {value: 4, label: '搜狗推广'}
        , {value: 5, label: '卧龙推广'}
        , {value: 6, label: '360点睛'}
      ],
      // 展示模式
      modelList: [{value: 1, label: '均衡展示'}
        , {value: 2, label: '随机展示'}
        , {value: 3, label: '权重展示'}
        , {value: 4, label: '轮番展示'}
      ],
      // 表格数据接口
      url: '/bazaar/read',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center'
        },
        {
          prop: 'name',
          label: '市场名称',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 110
        },
        {
          prop: 'platform',
          label: '推广平台',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 110,
          slot: 'platform'
        },
        {
          prop: 'model',
          label: '展示模式',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 110,
          slot: 'model'
        },
        {
          prop: 'sort',
          label: '排序',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 110
        },
        {
          prop: 'datatime',
          label: '创建时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 110,
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          prop: 'status',
          label: '状态',
          sortable: 'custom',
          width: 100,
          resizable: false,
          slot: 'status'
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 130,
          resizable: false,
          slot: 'action'
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
    };
  },
  created() {
    window.addEventListener("resize", this.getHeight);
    this.getHeight();
  },

  methods: {
    /*设置表格高度*/
    getHeight() {
      this.defaultHeight = window.innerHeight - 360;
    },

    /* 刷新表格 */
    reload(isPage) {
      let obj = {where: this.where};
      if (!isPage) {
        obj.page = 1;
      }
      this.$refs.table.reload(obj);
    },

    /* 重置搜索 */
    reset() {
      for (let key in this.where) {
        this.where[key] = null;
      }
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.delete('/bazaar/delete/id/' + row.id).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据')
        return;
      }
      this.$confirm('确定要删除选中的用户吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const iad = this.selection.map(d => d.id);
        const loading = this.$loading({lock: true});
        this.$http.delete('/bazaar/delete', {
          data: {id: iad.join(',')}
        }).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message({type: 'success', message: res.data.msg});
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
    /* 更改状态 */
    editState(row) {
      const loading = this.$loading({lock: true});
      this.$http.put('/bazaar/update/state/' + row.id, {status: row.status}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message({type: 'success', message: res.data.msg});
        } else {
          row.state = !row.state ? 1 : 0;
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    }
  }
}
</script>

<style scoped>

</style>
